home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_015 / clock / clock.c next >
C/C++ Source or Header  |  1992-05-06  |  4KB  |  157 lines

  1. /*
  2.  * clock - a dumb, digital clock in the upper right-hand corner. Designed
  3.  *    to be small, not flexible!
  4.  *
  5.  * Copyright (c) 1986, Mike Meyer
  6.  *
  7.  * Permission is hereby granted to distribute this program for any purposes
  8.  * whatsoever, so long as this notice, including the above copyright, is
  9.  * included with the distribution. Unlike other people, I don't care if you
  10.  * make money off of this, so long as I get credit for having written it.
  11.  */
  12.  
  13. #include <exec/types.h>
  14. #include <devices/timer.h>
  15. #include <libraries/dos.h>
  16. #include <intuition/intuition.h>
  17.  
  18. #include <stdio.h>
  19.  
  20. #define INTUITION_REV    1
  21. /*
  22.  * Things to tweak:
  23.  *    WIN_WIDTH - the width of the window output screen. Should be 136.
  24.  *    WAIT_TIME - how long to wait between updates. Also the maximum
  25.  *        mis-time you can get.  Finally, it's the longest period of
  26.  *        time you have to put up with the clock broken. Measured in
  27.  *        seconds.
  28.  */
  29. #define    WIN_WIDTH    136
  30. #define WAIT_TIME    15
  31.  
  32. static struct NewWindow    New_Window = {
  33.     (590 - WIN_WIDTH), 0,    /* Upper right, out of the way */
  34.     WIN_WIDTH, 10,        /* Just big enough for the time */
  35.     -1, -1,            /* Default pens */
  36.     CLOSEWINDOW,        /* All we care about is closing */
  37.     WINDOWCLOSE        /* Borderless, fairly standard window */
  38.     | WINDOWDEPTH | WINDOWDRAG | SMART_REFRESH | NOCAREREFRESH,
  39.     (struct Gagdet *) NULL,
  40.     (struct Image *) NULL,
  41.     "",            /* Empty title */
  42.     (struct Screen *) NULL,
  43.     (struct BitMap *) NULL,
  44.     0, 0, 0, 0,        /* no change sizes, doesn't matter */
  45.     WBENCHSCREEN        /* Of course! */
  46.     } ;
  47.  
  48. static char    Date_Buffer[8] ;    /* Now you know where the time goes! */
  49.  
  50. static struct IntuiText    Date_Text = {
  51.     1, 0,                /* Use the standard pen colors */
  52.     JAM2,                /* Use both of them */
  53.     29, 1,                /* in the upper left-hand corner */
  54.     (struct TextAttr *) NULL,    /* Use default text */
  55.     Date_Buffer,            /* Buffer for time */
  56.     (struct IntuiText *) NULL    /* All of text */
  57.     } ;
  58.  
  59. struct IntuitionBase    *IntuitionBase ;
  60.  
  61. /*
  62.  * Some things that need to be shared with done.
  63.  */
  64. static struct Window        *Window = NULL ;
  65. static struct timerequest    Time_Req ;
  66. static struct MsgPort        *Timer_Port = NULL, *CreatePort() ;
  67. #ifdef    DEBUG
  68. static short            cli = FALSE ;
  69. #endif
  70.  
  71. #ifdef    DEBUG
  72. main(argc, argv) int argc; char *argv; {
  73. #else
  74. #ifdef MANX
  75. main() {
  76. #else
  77. _main() {
  78. #endif
  79. #endif
  80.     register int        hours, minutes ;
  81.     register char        *buffer = Date_Buffer ;
  82.     struct DateStamp    now ;
  83.     struct IntuiMessage    *Msg, *GetMsg() ;
  84.  
  85. #ifdef    DEBUG
  86.     if (argc) cli = TRUE ;
  87. #endif
  88.     if ((IntuitionBase = (struct IntuitionBase *)
  89.         OpenLibrary("intuition.library", INTUITION_REV)) == NULL)
  90.         done(20, "Can't open Intuition library") ;
  91.  
  92.     if ((Timer_Port = CreatePort("Timer Port", 0)) == NULL)
  93.         done(20, "Can't create timer port") ;
  94.  
  95.     if (OpenDevice(TIMERNAME, UNIT_VBLANK, (char *) &Time_Req, 0) != NULL)
  96.         done(20, "Can't open timer device") ;
  97.     Time_Req . tr_node . io_Message . mn_ReplyPort = Timer_Port ;
  98.     Time_Req . tr_node . io_Command = TR_ADDREQUEST ;
  99.     Time_Req . tr_node . io_Flags = 0 ;
  100.     Time_Req . tr_node . io_Error = 0 ;
  101.  
  102.     if ((Window = (struct Window *) OpenWindow(&New_Window)) == NULL)
  103.         done(20, "Can't open window") ;
  104.     
  105.     buffer[3] = ':' ;
  106.     buffer [0] = buffer[6] = ' ' ;
  107.  
  108.     for (;;) {
  109.         DateStamp(&now) ;
  110.         hours = now . ds_Minute / 60 ;
  111.         minutes = now . ds_Minute % 60 ;
  112.         buffer[1] = hours / 10 + '0' ;
  113.         if (buffer[1] == '0')        /* Blank out a possible */
  114.             buffer[1] = ' ' ;    /* leading 0 */
  115.         buffer[2] = hours % 10 + '0' ;
  116.         buffer[4] = minutes / 10 + '0' ;
  117.         buffer[5] = minutes % 10 + '0' ;
  118.  
  119.         PrintIText(Window -> RPort, &Date_Text, 0, 0) ;
  120.  
  121.         Time_Req . tr_time . tv_secs = WAIT_TIME ;
  122.         Time_Req . tr_time . tv_micro = 0 ;
  123.         SendIO((char *) &Time_Req . tr_node) ;
  124.         Wait(1 << Window -> UserPort -> mp_SigBit
  125.            | 1 << Timer_Port -> mp_SigBit) ;
  126.  
  127.         while (Msg = GetMsg(Window -> UserPort)) {
  128.             if (Msg -> Class == CLOSEWINDOW) {
  129.                 ReplyMsg(Msg) ;
  130.                 done(0, "exit") ;
  131.                 }
  132.             ReplyMsg(Msg) ;
  133.             }
  134.  
  135.         (void) GetMsg(Timer_Port) ;
  136.         }
  137.     /* NOTREACHED */
  138.     }
  139. /*
  140.  * done - just clean up that which is open, and then leave.
  141.  */
  142. done(how, why) int how; char *why; {
  143.  
  144.     AbortIO((char *) &Time_Req . tr_node) ;
  145.     if (Window) CloseWindow(Window) ;
  146.     if (Time_Req . tr_node . io_Message . mn_ReplyPort)
  147.         CloseDevice(&Time_Req) ;
  148.     if (Timer_Port) DeletePort(Timer_Port) ;
  149.     if (IntuitionBase) CloseLibrary(IntuitionBase) ;
  150. #ifdef    DEBUG
  151.     if (cli) printf("clock: %s\n", why) ;
  152. #endif
  153.  
  154.     OpenWorkBench() ;            /* As requested */
  155.     exit(how) ;
  156.     }
  157.